home *** CD-ROM | disk | FTP | other *** search
/ Merciful 5 / Merciful - Disc 5.iso / software / r / rtg_master / rtgmasterv21.0dev.lha / demos / network / P2PServ.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-28  |  1.9 KB  |  84 lines

  1. #include <clib/socket_protos.h>
  2. #include <pragmas/socket_pragmas.h>
  3. #include <clib/rtgmaster_protos.h>
  4. #include <pragmas/rtgmaster_pragmas.h>
  5. #include <rtgmaster/rtgmaster.h>
  6. #include <rtgmaster/rtgsublibs.h>
  7. #include <clib/exec_protos.h>
  8. #include <pragmas/exec_pragmas.h>
  9. #include <stdio.h>
  10. #include <netdb.h>
  11. #include <rtgmaster/rtgtcpip.h>
  12. #define UDPDemo 1
  13.  
  14. // If you want to compile this demo to use
  15. // TCP instead of UDP, set UDPDemo to 0
  16.  
  17. struct Library *SocketBase=0;
  18. struct Library *RTGMasterBase=0;
  19.  
  20. void main()
  21. {
  22.  if (RTGMasterBase=OpenLibrary("rtgmaster.library",0))
  23.  {
  24.   if (SocketBase=OpenLibrary("bsdsocket.library",0))
  25.   {
  26.    struct RTG_Socket *rs;
  27.    struct RTG_Socket *msgsock;
  28.    char buf[1024];
  29.    int rval;
  30.    long mode=1;
  31.  
  32. #ifndef UDPDemo
  33.    rs=OpenServer(SocketBase,3056,SOCK_STREAM,0);
  34. #else
  35.    rs=OpenServer(SocketBase,3056,SOCK_DGRAM,0);
  36. #endif
  37.  
  38.    //RtgIoctl(SocketBase,rs,&mode);
  39.  
  40.    while(1)
  41.    {
  42.  
  43. #ifndef UDPDemo
  44.     msgsock=RtgAccept(SocketBase,rs);
  45. #else
  46.     msgsock=rs;
  47. #endif
  48.  
  49.     do {
  50.         struct sockaddr_in *si;
  51.  
  52. // Note : As GetUDPName returns 0 for TCP connections, this code
  53. // works for TCP *and* UDP ...
  54.  
  55. // If you want a Connection with Multiple Client, WITHOUT
  56. // using RunServer (works only with UDP...) you can simply
  57. // open multiple Clients and then test, from which Client a
  58. // Message came using RtgInAdr (it can't differentiate
  59. // between multiple Clients running on the same machine, BTW).
  60.  
  61.         si=GetUDPName(SocketBase,msgsock);
  62.         rval=RtgRecv(SocketBase,msgsock,buf,si,1024);
  63.  
  64. #ifndef UDPDemo
  65.  
  66. #else
  67.         if (rval>0) printf("Message from : %s\n",RtgInAdr(SocketBase,si));
  68. #endif
  69.  
  70.         if (rval>0) RtgSend(SocketBase,msgsock,buf,si,rval);
  71.        } while (rval>0);
  72.  
  73. #ifndef UDPDemo
  74.     CloseClient(SocketBase,msgsock);
  75. #endif
  76.  
  77.    }
  78.    if (SocketBase) CloseLibrary(SocketBase);
  79.    if (RTGMasterBase) CloseLibrary(RTGMasterBase);
  80.   }
  81.  }
  82. }
  83.  
  84.